package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import util.Utilities;

import beans.Userbean;

import dao.User;

public class UserManageServlet extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	this.doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
	if (request.getQueryString() != null) {
	    Userbean userService = new Userbean();
	    String actionString = request.getParameter("action");
	    if (actionString.equals("getUserExists")) {
		String usernameString = request.getParameter("username").trim();
		if (userService.userExists(usernameString)) {
		    response.getWriter().write("exists");
		} else {
		    response.getWriter().write("notexists");
		}

	    } else if (actionString.equals("modify")) {
		String password = request.getParameter("password").trim();
		String username = request.getParameter("username").trim();
		User user1 = (User) request.getSession().getAttribute("user");
		user1.setPassword(password);
		user1.setUsername(username);
		if (userService.modifyUser(user1)) {
		    HttpSession session = request.getSession();
		    session.removeAttribute("user");
		    if (session.getAttribute("grade") != null) {
			session.removeAttribute("grade");
		    }
		    if (session.getAttribute("course") != null) {
			session.removeAttribute("course");
		    }
		    session.invalidate();
		    request.setAttribute("success", "true");
		} else {
		    request.setAttribute("success", "false");
		}
		request.getRequestDispatcher("../modifyUser.jsp").forward(
			request, response);
	    } else if (actionString.equals("add")) {
		String username = request.getParameter("username").trim();
		String password = request.getParameter("password").trim();

		User user1 = new User();
		user1.setUsername(username);
		user1.setPassword(password);
		user1.setUsertype(1);// 学生
		user1.setUserid(Utilities.getUserId());

		if (userService.addUser(user1)) {
		    // request.getSession().invalidate();
		    request.setAttribute("success", "true");
		} else {
		    request.setAttribute("success", "false");
		}
		request.getRequestDispatcher("../regesiter.jsp").forward(
			request, response);
	    } else if (actionString.equals("reset")) {
		String userid = request.getParameter("userId");
//		if (
			userService.resetPassword(userid);
//			) {
		    request.setAttribute("success", "true");
//		} else {
//		    request.setAttribute("success", "false");
//		}
		response.sendRedirect("../admin/userList.jsp");
	    }
	}
    }
}
